﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class Employee_CreateVacancy : System.Web.UI.Page
{
    EmployeeMethord EM = new EmployeeMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        btnCreate.Enabled = false;
        txtDateCreation.Text = DateTime.Now.ToShortDateString();
        txtDateFilfilled_CalendarExtender.StartDate = DateTime.Now;
        if (!IsPostBack)
        {
            SetDDLTypeOfWork();
        }
    }
    public void SetDDLTypeOfWork()
    {
        try
        {
            if (ddlTypeJob.Items.Count < 2)
            {
                SqlDataReader dr = EM.ReturnTypeOfJob();
                ddlTypeJob.DataSource = dr;
                ddlTypeJob.DataTextField = "JobName";
                ddlTypeJob.DataValueField = "JobID";
                ddlTypeJob.DataBind();
                ddlTypeJob.Items.Insert(0, "Please choise Type of work");
                EM.connClose();
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        if (ddlTypeJob.SelectedValue.Equals("Please choise Type of work"))
        {
            lblTypeofworkError.Text = "(*)";
            lblError.Text = "Check type of work.";
            ddlTypeJob.Focus();
        }
        else if (txtTitle.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "(*)";
            lblError.Text = "Tilte vacancy can't be empty";
        }
        else if (txtDecription.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "(*)";
            lblError.Text = "Description can't be empty.";
            txtDecription.Focus();
        }
        else if (txtNumberofJob.Text.Equals("") || !(Regex.IsMatch(txtNumberofJob.Text, @"[0-9]*\.?[0-9]+$")))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "(*)";
            lblError.Text = "Check Number of job.";
            txtNumberofJob.Focus();
        }
        else if (txtSalary.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblErrorSalary.Text = "(*)";
            lblError.Text = "Salary can't be empty.";
            txtSalary.Focus();
        }
        else if (txtAddress.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblErrorSalary.Text = "";
            lblErrorAddress.Text = "(*)";
            lblError.Text = "Address workplace can't be empty.";
            txtSalary.Focus();
        }
        else if (txtDateFilfilled.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblErrorSalary.Text = "";
            lblErrorAddress.Text = "";
            lblDateError.Text = "(*)";
            lblError.Text = "Date Fulfilled can't be empty.";
            txtDateFilfilled.Focus();
        }
        else
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblDateError.Text = "";
            lblErrorSalary.Text = "";
            lblErrorAddress.Text = "";
            lblError.Text = "";
            try
            {
                DateTime date = new DateTime();
                date = Convert.ToDateTime(txtDateFilfilled.Text);
                bool bol = EM.InsertTableVacancy(txtTitle.Text, Convert.ToInt32(ddlTypeJob.SelectedValue), Session["User"].ToString(), txtDecription.Text, Convert.ToInt32(txtNumberofJob.Text), txtSalary.Text, txtAddress.Text, DateTime.Now, Convert.ToDateTime(txtDateFilfilled.Text));
                if (bol)
                {
                    lblSuccess.Text = "Added new vacancy: " + txtTitle.Text;
                }
                else
                {
                    lblError.Text = "Add new vacancy failed.";
                }
            }
            catch (Exception)
            {
                lblDateError.Text = "(*)";
                lblError.Text = "Check Date Fulfilled.";
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ddlTypeJob.ClearSelection();
        txtTitle.Text = "";
        txtDecription.Text = "";
        txtDateFilfilled.Text = "";
        txtNumberofJob.Text = "";
        txtSalary.Text = "";
        txtAddress.Text = "";
        lblTypeofworkError.Text = "";
        lblTitleError.Text = "";
        lblDescriptError.Text = "";
        lblDateError.Text = "";
        lblNumber.Text = "";
        lblError.Text = "";
        lblSuccess.Text = "";
    }
}